#include "AnaDef.h"
#include "XGLUtils.h"

Bool_t kgrid = kFALSE;

TGraphErrors * ClearGraph(const TGraphErrors *gr0, const Int_t di)
{
  if(gr0->GetN()<60)
    return 0x0;

  Int_t np=0;
  TGraphErrors *grnew=new TGraphErrors;
  for(Int_t ii=0; ii<gr0->GetN(); ii++){
    if(ii%di){
      continue;
    }
    Double_t xx=-999, yy=-999;
    gr0->GetPoint(ii,xx,yy);
    const Double_t ex = gr0->GetErrorX(ii);
    const Double_t ey = gr0->GetErrorY(ii);
    grnew->SetPoint(np, xx, yy);
    grnew->SetPointError(np, ex, ey);
    printf("%d: %10.6f %10.6f   %10.6f %10.6f\n",np, xx, ex, yy, ey);
    np++;
  }
  grnew->SetName(Form("%s%d",gr0->GetName(), di));
  grnew->SetTitle(gr0->GetTitle());
  return grnew;
}

void doEfficiency(const TString tag, const TString fin0, const TString fin1, const TString fin2)
{
  const TString fins[]={fin0, fin1, fin2};
  const Int_t cols[]={kRed, kBlue, kBlack};
  const Int_t stys[]={22, 23, 20, 21, 29};

  const Int_t nfile = 3;
  TGraphErrors * grs[]={0x0,0x0,0x0};
  for(Int_t ii=0; ii<nfile; ii++){
    TFile ff(fins[ii]);
    printf("\nFile: %s\n", fins[ii].Data());
    TGraphErrors * gtmp = (TGraphErrors*)ff.Get("rELtrd");

    if(!gtmp){
      ff.ls();
      printf("graph not found!\n"); exit(1);
    }

    grs[ii] = ClearGraph(gtmp, 3);
  }

  TCanvas *cc=new TCanvas("cc");
  XGLUtils::PadSetup(cc, 0.12, 0.04, 0.04, 0.13);
  
  if(kgrid) cc->SetGrid();
  TLegend *lmpv=new TLegend(0.13, 0.68, 0.83, 0.88);
  XGLUtils::ResetStyle(lmpv);

  const TString head("TRD dE/dx+TR ");
  lmpv->SetHeader(head + (tag.Contains("type2")?"(pp,#sqrt{s}=7 TeV)":"(PbPb,#sqrt{s}=2.76 ATeV, Centrality 0-20%)"));
  for(Int_t ii=0; ii<nfile; ii++){
    if(!grs[ii])
      continue;

    grs[ii]->SetMarkerStyle(stys[ii]);
    grs[ii]->SetMarkerColor(cols[ii]);
    grs[ii]->SetLineColor(cols[ii]);
    grs[ii]->GetYaxis()->SetTitle("Pion Efficiency");
    grs[0]->SetMaximum(1);
    grs[0]->SetMinimum(5e-3);
    gPad->SetLogy();
    grs[ii]->GetXaxis()->SetTitle(tag.Contains("par0")?"Electron Efficiency":"Proton Efficiency");
    grs[ii]->GetXaxis()->SetLimits(0.5, 1);

    XGLUtils::ResetStyle(grs[ii]);

    grs[ii]->Draw(ii?"lp":"alp"); 
    lmpv->AddEntry(grs[ii], grs[ii]->GetTitle(), "lp");
  }
  lmpv->Draw();

  XGLUtils::DrawLogo(cc, 0.15, 0.45, 0.28, 0.65, "10/03/2012");
 
  cc->Print(tag+"performance_efficiency.png");
  cc->Print(tag+"performance_efficiency.eps");

  TFile *fout=new TFile(Form("%s_efficiency_Q%d.root", tag.Data(), fin0.Contains("Q1")),"recreate");
  for(Int_t ii=0; ii<nfile; ii++){
    if(!grs[ii])
      continue;

    grs[ii]->Write();
  }
  cc->Write();
  fout->Save();
  fout->Close();
}

int main(const int argc, char *argv[])
{
  for(int ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }

  if(argc!=4  &&  argc!=5){
    printf("argc!=4 or 5\n");
    return 1;
  }

  if(argc==5){
    kgrid = atoi(argv[4]);
    printf("\nsetting kgrid %d\n\n", kgrid);
  }

  TString fin0(argv[1]);
  TString tag;
  if(fin0.Contains("par0"))
    tag+="par0";
  else if(fin0.Contains("par4"))
    tag+="par4";
  else{
    printf("wrong par! %s\n", fin0.Data());exit(1);
  }

  if(fin0.Contains("type2"))
    tag+="type2";
  else if(fin0.Contains("type4"))
    tag+="type4";
  else{
    printf("wrong type! %s\n", fin0.Data());exit(1);
  }

  XGLUtils::SetGlobalStyle();
  doEfficiency(tag, argv[1], argv[2], argv[3]);

  return 0;
}
